package com.example;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Liuqz
 * @date 2018/11/16 20:10
 */
public class StreamInAction {

    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        //1. Find all transactions in the year 2011 and sort them by value (small to high).
        List<Transaction> transactionList = transactions.stream().filter(transaction -> transaction.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());

        System.out.println(transactionList);

        //2.What are all the unique cities where the traders work?
        transactions.stream().map(t -> t.getTrader().getCity())
                .distinct().forEach(System.out::print);

        System.out.println("=============================================");

        //3.Find all traders from Cambridge and sort them by name.
        transactions.stream().map(Transaction::getTrader)
                .filter(f -> f.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .forEach(System.out::println);

        System.out.println("=============================================");

        //4.Return a string of all traders’ names sorted alphabetically
        String value = transactions.stream().map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("", (name1, name2) -> name1 + name2);
        System.out.println(value);

        //5.Are any traders based in Milan?
        boolean liveInMilan = transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("Milan"));
        System.out.println("=============================================");


        //6.Print all transactions’ values from the traders living in Cambridge.
        transactions.stream().filter(e -> e.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .forEach(System.out::println);
        System.out.println("=============================================");


        //7.What’s the highest value of all the transactions?
        int max = transactions.stream()
                .mapToInt(e -> e.getValue()).max().getAsInt();
        System.out.println(max);
        System.out.println("=============================================");


        //8.Find the transaction with the smallest value.
        transactions.stream().map(e -> e.getValue())
                .reduce(Integer::min).ifPresent(System.out::println);
    }
}
